﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using smartphone.SecurityLib;
using System.Web.Profile;

namespace smartphone.App_Code
{
    /// <summary>
    /// 
    /// chua thong tin nguoi dung bao gom ca thong tin tai khoan
    /// </summary>
    public class ProfileWrapper
    {
        private string address1;
        private string address2;
        private string city;
        private string region;
        private string postalCode;
        private string country;
        private string shippingRegion;
        private string dayPhone;
        private string evePhone;
        private string mobPhone;
        private string email;
        private string creditCard;
        private string creditCardHolder;
        private string creditCardNumber;
        private string creditCardIssueDate;
        private string creditCardIssueNumber;
        private string creditCardExpiryDate;
        private string creditCardType;

        public string Address1
        {
            get
            {
                return address1;
            }
            set
            {
                address1 = value;
            }
        }
        public string Address2
        {
            get
            {
                return address2;
            }
            set
            {
                address2 = value;
            }
        }
        public string City
        {
            get
            {
                return city;
            }
            set
            {
                city = value;
            }
        }
        public string Region
        {
            get
            {
                return region;
            }
            set
            {
                region = value;
            }
        }
        public string PostalCode
        {
            get
            {
                return postalCode;
            }
            set
            {
                postalCode = value;
            }
        }
        public string Country
        {
            get
            {
                return country;
            }
            set
            {
                country = value;
            }
        }
        public string ShippingRegion
        {
            get
            {
                return shippingRegion;
            }
            set
            {
                shippingRegion = value;
            }
        }
        public string DayPhone
        {
            get
            {
                return dayPhone;
            }
            set
            {
                dayPhone = value;
            }
        }
        public string EvePhone
        {
            get
            {
                return evePhone;
            }
            set
            {
                evePhone = value;
            }
        }
        public string MobPhone
        {
            get
            {
                return mobPhone;
            }
            set
            {
                mobPhone = value;
            }
        }
        public string Email
        {
            get
            {
                return email;
            }
            set
            {
                email = value;
            }
        }
        public string CreditCard
        {
            get
            {
                return creditCard;
            }
            set
            {
                creditCard = value;
            }
        }
        public string CreditCardHolder
        {
            get
            {
                return creditCardHolder;
            }
            set
            {
                creditCardHolder = value;
            }
        }
        public string CreditCardNumber
        {
            get
            {
                return creditCardNumber;
            }
            set
            {
                creditCardNumber = value;
            }
        }
        public string CreditCardIssueDate
        {
            get
            {
                return creditCardIssueDate;
            }
            set
            {
                creditCardIssueDate = value;
            }
        }
        public string CreditCardIssueNumber
        {
            get
            {
                return creditCardIssueNumber;
            }
            set
            {
                creditCardIssueNumber = value;
            }
        }
        public string CreditCardExpiryDate
        {
            get
            {
                return creditCardExpiryDate;
            }
            set
            {
                creditCardExpiryDate = value;
            }
        }
        public string CreditCardType
        {
            get
            {
                return creditCardType;
            }
            set
            {
                creditCardType = value;
            }
        }

        public ProfileWrapper()
        {
            //ProfileCommon profile = HttpContext.Current.Profile as ProfileCommon;
            address1 = HttpContext.Current.Profile.GetPropertyValue("Address1").ToString();
            address2 = HttpContext.Current.Profile.GetPropertyValue("Address2").ToString();
            city = HttpContext.Current.Profile.GetPropertyValue("City").ToString();
            region = HttpContext.Current.Profile.GetPropertyValue("Region").ToString();
            postalCode = HttpContext.Current.Profile.GetPropertyValue("PostalCode").ToString();
            country = HttpContext.Current.Profile.GetPropertyValue("Country").ToString();
            //shippingRegion = (HttpContext.Current.Profile.GetPropertyValue("ShippingRegion") == null
            //  || HttpContext.Current.Profile.GetPropertyValue("ShippingRegion").ToString() == "" ? "1" : HttpContext.Current.Profile.GetPropertyValue("ShippingRegion").ToString());
            dayPhone = HttpContext.Current.Profile.GetPropertyValue("DayPhone").ToString();
            evePhone = HttpContext.Current.Profile.GetPropertyValue("EvePhone").ToString();
            mobPhone = HttpContext.Current.Profile.GetPropertyValue("MobPhone").ToString();
            email = Membership.GetUser(HttpContext.Current.Profile.UserName).Email == null ? "" : Membership.GetUser(HttpContext.Current.Profile.UserName).Email;

            try
            {
                SecureCard secureCard = new SecureCard(HttpContext.Current.Profile.GetPropertyValue("CreditCard").ToString());
                creditCard = secureCard.CardNumberX;
                creditCardHolder = secureCard.CardHolder;
                creditCardNumber = secureCard.CardNumber;
                creditCardIssueDate = secureCard.IssueDate;
                creditCardIssueNumber = secureCard.IssueNumber;
                creditCardExpiryDate = secureCard.ExpiryDate;
                creditCardType = secureCard.CardType;
            }
            catch
            {
                creditCard = "Not entered.";
            }

        }

        public void UpdateProfile()
        {
            //var profile = HttpContext.Current.Profile;
            HttpContext.Current.Profile.SetPropertyValue("Address1", address1);
            HttpContext.Current.Profile.SetPropertyValue("Address2", address2);
            HttpContext.Current.Profile.SetPropertyValue("City", city);
            HttpContext.Current.Profile.SetPropertyValue("Region", region);
            HttpContext.Current.Profile.SetPropertyValue("PostalCode", postalCode);
            HttpContext.Current.Profile.SetPropertyValue("Country", country);
            //HttpContext.Current.Profile.SetPropertyValue("ShippingRegion", shippingRegion);
            HttpContext.Current.Profile.SetPropertyValue("DayPhone", dayPhone);
            HttpContext.Current.Profile.SetPropertyValue("EvePhone", evePhone);
            HttpContext.Current.Profile.SetPropertyValue("MobPhone", mobPhone);
            HttpContext.Current.Profile.SetPropertyValue("CreditCard", creditCard);
            MembershipUser user = Membership.GetUser(HttpContext.Current.Profile.UserName);
            user.Email = email;
            Membership.UpdateUser(user);
            try
            {
                SecureCard secureCard = new SecureCard(
                   creditCardHolder, creditCardNumber,
                   creditCardIssueDate, creditCardExpiryDate,
                   creditCardIssueNumber, creditCardType);
                HttpContext.Current.Profile.SetPropertyValue("CreditCard", secureCard.EncryptedData);
            }
            catch
            {
                creditCard = "";
            }
        }

    }
}